<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 Netty 高性能传输模型</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 10px 20px;
            font-size: 14px;
            color: #888;
            border-bottom: 1px solid #444;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
            font-size: 14px;
            line-height: 1.6;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
            margin: 40px 0 20px 0;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 20px 30px;
            border-radius: 12px;
            margin: 30px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: pulse 3s ease-in-out infinite;
        }
        
        @keyframes pulse {
            0%, 100% { transform: scale(1); opacity: 0.5; }
            50% { transform: scale(1.1); opacity: 0.3; }
        }
        
        .feature-card {
            background: white;
            border-radius: 16px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
            border: 1px solid #e5e7eb;
        }
        
        .feature-card:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            border-color: #667eea;
        }
        
        .icon-wrapper {
            width: 60px;
            height: 60px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
        }
        
        .mermaid {
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            margin: 30px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e5e7eb;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    深入理解 Netty<br>
                    <span class="text-3xl md:text-4xl font-light opacity-90">高性能传输模型的架构与实现</span>
                </h1>
                <p class="text-xl opacity-90 leading-relaxed">
                    探索事件驱动、异步非阻塞 I/O 和零拷贝技术的完美结合
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-rocket text-3xl mb-2"></i>
                        <p class="text-sm">高性能</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-network-wired text-3xl mb-2"></i>
                        <p class="text-sm">低延迟</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-layer-group text-3xl mb-2"></i>
                        <p class="text-sm">高并发</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="content-card rounded-2xl p-8 md:p-12 mb-12 max-w-5xl mx-auto">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">N</span>etty 是一个高性能的网络通信框架，它提供了高效的传输模型，通过抽象和优化，能够处理高并发、低延迟的网络通信。在 Netty 中，传输模型的核心包括事件驱动模型、网络协议的解码/编码、数据传输的底层实现等。Netty 使用了许多优化机制，例如零拷贝、NIO、I/O 多路复用等，使得数据传输变得非常高效。
            </p>
        </div>

        <!-- Architecture Overview -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-cube text-purple-600 mr-3"></i>
                Netty 传输模型的架构概述
            </h2>
            
            <div class="content-card rounded-2xl p-8 mb-8">
                <p class="text-gray-700 leading-relaxed mb-6">
                    Netty 的传输模型基于事件驱动和异步非阻塞的 I/O 机制，依赖于 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">EventLoop</code>、<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">Channel</code> 和 <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">ChannelPipeline</code> 来处理事件和 I/O 操作。
                </p>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="feature-card">
                        <div class="icon-wrapper">
                            <i class="fas fa-broadcast-tower text-white text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">Channel</h3>
                        <p class="text-gray-600">代表一个网络连接，负责与远程服务器建立连接，发送数据，接收数据等。</p>
                    </div>
                    
                    <div class="feature-card">
                        <div class="icon-wrapper">
                            <i class="fas fa-stream text-white text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">ChannelPipeline</h3>
                        <p class="text-gray-600">负责处理数据流的各个阶段，包含一系列的 ChannelHandler。</p>
                    </div>
                    
                    <div class="feature-card">
                        <div class="icon-wrapper">
                            <i class="fas fa-sync-alt text-white text-2xl"></i>
                        </div>
                        <h3 class="text-xl font-bold mb-3">EventLoop</h3>
                        <p class="text-gray-600">用于执行所有 I/O 操作的事件循环，保证异步非阻塞处理。</p>
                    </div>
                </div>
                
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>Channel 接口定义
                    </div>
                    <pre><code class="language-java">public interface Channel {
    ChannelFuture connect(SocketAddress remoteAddress);
    ChannelFuture write(Object msg);
    ChannelPipeline pipeline();
}</code></pre>
                </div>
            </div>
        </div>

        <!-- EventLoop Section -->
        <div class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800">
                <i class="fas fa-circle-notch text-purple-600 mr-3"></i>
                EventLoop 和 EventLoopGroup 的实现
            </h2>
            
            <div class="content-card rounded-2xl p-8">
                <p class="text-gray-700 leading-relaxed mb-6">
                    <code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">EventLoop</code> 是 Netty 的核心组件之一，它是一个事件循环的实现，负责处理事件的调度和执行。<code class="bg-purple-100 text-purple-700 px-2 py-1 rounded">EventLoopGroup</code> 则是 EventLoop 的容器，管理多个 EventLoop 实例，确保 I/O 操作的并发处理。
                </p>
                
                <div class="mermaid">
                    <script type="text/javascript">
                        mermaid.initialize({ startOnLoad: true, theme: 'default' });
                    </script>
                    <div class="mermaid">
                        graph TD
                            A[EventLoopGroup] --> B[EventLoop 1]
                            A --> C[EventLoop 2]
                            A --> D[EventLoop N]
                            B --> E[Selector]
                            B --> F[Task Queue]
                            E --> G[Channel 1]
                            E --> H[Channel 2]
                            E --> I[Channel N]
                            style A fill:#667eea,stroke:#764ba2,stroke-width:2px,color:#fff
                            style B fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                            style C fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                            style D fill:#f093fb,stroke:#f5576c,stroke-width:2px,color:#fff
                    </div>
                </div>
                
                <div class="code-block mt-8">
                    <div class="code-header">
                        <i class="fas fa-code mr-2"></i>NioEventLoop 实现
                    </div>
                    <pre><code class="language-java">public class NioEventLoop implements EventLoop {
    private final Selector selector;  // NIO选择器

    public NioEventLoop(EventLoopGroup parent) {
        this.selector = Selector.open();  // 初始化选择器
    }

    @Override
    public void run() {
        while (true) {
            try {
                // 处理所有事件
                selector.select();
                // 处理已就绪的通道
                processSelectedKeys();